﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Media;

namespace MyPoints.UserControls
{
  public class AccordionExpanderExtension
  {
    private List<Expander> exps;
    private Expander expandedOne = null;
    private SolidColorBrush expandedTitleBackgroundBrush = new SolidColorBrush(Color.FromRgb(200, 200, 200));
    private SolidColorBrush storedColorBrush = null;

    public AccordionExpanderExtension(StackPanel pnlAccordion, Expander initiallyExpandedExpander)
    {

      exps = pnlAccordion.Children.OfType<Expander>().ToList();
      exps.ForEach(q => q.IsExpanded = false);
      exps.ForEach(q => q.Expanded += exp_Expanded);
      exps.ForEach(q => q.Collapsed += exp_Collapsed);

      initiallyExpandedExpander.IsExpanded = true;
    }

    private void exp_Collapsed(object sender, System.Windows.RoutedEventArgs e)
    {
      if (sender == expandedOne)
        expandedOne = null;
    }

    private void exp_Expanded(object sender, System.Windows.RoutedEventArgs e)
    {
      if (expandedOne != null)
      {
        expandedOne.IsExpanded = false;
      }
      expandedOne = sender as Expander;
    }

    internal void Expand(Expander expander)
    {
      if (expandedOne != null)
      {
        expandedOne.IsExpanded = false;
      }

      expander.IsExpanded = true;
    }
  }
}
